layui.define(['Mock', 'apiUser', 'qs'], function (exports) {
	var Mock = layui.Mock,
		api = layui.apiUser.api,
		qs = layui.qs;
	Mock.Random.extend({
		phone: function () {
			var phonePrefixs = ['132', '135', '189']
			return this.pick(phonePrefixs) + Mock.mock(/\d{8}/)
		}
	})

	var userMeta = {
		id: '@integer(2, 1000)',
		name: '@cname',
		'sex|0-1': 0,
		birthday: '@date',
		mobile: '@phone',
		'role|+1': [
			{id: 2, name: '管理员'},
			{id: 3, name: '普通成员'},
			{id: 4, name: '测试成员'},
		],
		head: '@dataImage("18x18", "icon")',
		org: {id: '@integer(2, 100)', name: '@ctitle(10,15)'},
		dept: {id: '@integer(2, 100)', name: '@cword(3,6)'},
		'disable|0-1': 0,
	};

	function userMock() {
		Mock.setup({
			timeout: '50-100'
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 20:49
		 * @description: 新增
		 */
		Mock.mock(RegExp(api.create + '[^\.]*$'), 'post', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body);
			return {
				code: 2000,
				msg: '新增用户成功',
			};
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 20:53
		 * @description: 删除
		 */
		Mock.mock(RegExp(api.remove + '[^\.]*$'), 'delete', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body);
			return {
				code: 2000,
				msg: '删除用户成功',
			};
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 20:54
		 * @description: 更新
		 */
		Mock.mock(RegExp(api.update + '[^\.]*$'), 'update', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body);
			return {
				code: 2000,
				msg: '更新用户成功',
			};
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 21:06
		 * @description: 列表
		 */
		Mock.mock(RegExp(api.list + '[^\.]*$'), function (options) {
			console.log(1111);
			return Mock.mock({
				code: 2000,
				msg: '获取列表成功',
				'data|4-15': [userMeta]
			});
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 21:06
		 * @description: 分页查询
		 */
		Mock.mock(RegExp(api.getPageList + '[^.]*$'), function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				total = 'total|' + params.pageSize + '-1000',
				list = 'list|' + params.pageSize,
				data = Mock.mock({
					[total]: 0,
					currentPage: params.currentPage,
					[list]: [userMeta]
				});
			return {
				code: 2000,
				msg: '获取列表成功',
				data: data
			}
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 21:06
		 * @description: 增强型分页查询
		 */
		Mock.mock(RegExp(api.postPageList + '[^\.]*$'), 'post', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body),
				param = body.param,
				total = 'total|' + params.pageSize + '-1000',
				list = 'list|' + params.pageSize,
				data = Mock.mock({
					[total]: 0,
					currentPage: params.currentPage,
					[list]: [userMeta]
				});
			return {
				code: 2000,
				msg: '获取列表成功',
				data: data
			}
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 21:07
		 * @description: 获取详情
		 */
		Mock.mock(RegExp(api.detail + '[^\.]*$'), function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				data = JSON.parse(options.body),
				id = params.id;
			return Mock.mock({
				code: 2000,
				msg: '查询成功',
				data: userMeta
			})
		})

		/**
		 * @author: ares
		 * @date: 2021/4/27 21:07
		 * @description: 当前登录用户信息
		 */
		Mock.mock(RegExp(api.getCurrentUserInfo + '[^\.]*$'), function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				data = JSON.parse(options.body);
			return Mock.mock({
				code: 2000,
				msg: '查询成功',
				data: {
					id: 1,
					name: 'Ares',
					sex: 1,
					birthday: '1983-05-29',
					mobile: 13166103561,
					role: {id: 1, name: '超级管理员'},
					head: '',
					org: {id: 1, name: '系统平台'},
					dept: {id: 1, name: 'IT部门'},
				}
			})
		})

		/**
		 * @author: ares
		 * @date: 2021/4/28 11:27
		 * @description: 启用禁用用户
		 */
		Mock.mock(RegExp(api.disable + '[^\.]*$'), 'put', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body);
			return {
				code: 2000,
				msg: '操作成功',
			}
		})
		/**
		 * @author: ares
		 * @date: 2021/4/28 11:28
		 * @description: 重置用户密码
		 */
		Mock.mock(RegExp(api.resetPwd + '[^\.]*$'), 'put', function (options) {
			var params = qs.parse(options.url.split('?')[1]),
				body = JSON.parse(options.body);
			return {
				code: 2000,
				msg: '重置密码成功',
			}
		})
	}

	exports("userMock", userMock);
})